import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/*
 * Reader.java - This class reads HTML codes of the websites and
 * 				 gives information to the program when de-manded.
 * 
 * @Author : BilCIS
 */

public class Reader 
{
	// properties
	private String iU;
	private URL web;
	
	// constructor
	public Reader ( String link ) throws KeyManagementException, NoSuchAlgorithmException, IOException
	{	
		try
		{
			web = new URL ( link );
		}
		catch ( Exception e )
		{
			JOptionPane.showMessageDialog(null, e.getMessage() );
		}
		
		iU = read();
	}
	
	// second constructor
	public Reader ( String link, String abc ) throws KeyManagementException, NoSuchAlgorithmException, IOException
	{	
		try
		{
			web = new URL ( link );
		}
		catch ( Exception e )
		{
			JOptionPane.showMessageDialog(null, e.getMessage() );
		}

	}
	
	// gets picture from web
	public ImageIcon getPic()
	{
		 return new ImageIcon(web);
	}

	// reads the web site
	public String read () throws IOException, KeyManagementException, NoSuchAlgorithmException
	{
		TrustManager[] trustAllCerts = new TrustManager[] {
			     new X509TrustManager() {
			          public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			            return null;
			          }
			          public void checkClientTrusted(X509Certificate[] certs, String authType) {  }
			          public void checkServerTrusted(X509Certificate[] certs, String authType) {  }
			       }
		};

		SSLContext sc = SSLContext.getInstance("SSL");
		sc.init(null, trustAllCerts, new java.security.SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

		// Create all-trusting host name verifier
		HostnameVerifier allHostsValid = new HostnameVerifier() {
		public boolean verify(String hostname, SSLSession session) {
			return true;
			}
		};
		// Install the all-trusting host verifier
		HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
		/*
		 * end of the fix -control security
		*/		   
			    
		BufferedReader in = new BufferedReader( new InputStreamReader( web.openStream() ) );
		
		String getLine;
	    while ( ( getLine = in.readLine() ) != null)
	         iU = iU + getLine + "\n";		
	     in.close();
	       
	     return iU;
	}
	
	// gets the normal string 
	public String getPageContents( String HTML )
	{
		String original = HTML;
    	String omitted = "";
    	boolean test1 = false; // test for < > parts
    	boolean test2 = false; // test for & ; parts
    	
   		for(int i = 0; i < original.length(); i++)
   		{
   			//to stop the adding letters
        	if( original.charAt(i) == '<' )
        	{
            	test1 = true;
       		}
       		//to stop the adding letters
       		if( original.charAt(i) == '&' )
       		{
       			test2 = true;
       		}
       		// adds letters if it test1 and test2 is appropriate
        	if( test1 == false && test2== false )
        	{
            	omitted += original.charAt(i);
       		}
       		//to continue the adding letters
        	if( original.charAt(i) == '>' )
        	{
            	test1 = false;
       		}
       		//to continue the adding letters 
        	if( original.charAt(i) == ';' )
        	{
        		test2 = false;
        	}	
    	}
   		return omitted;	
	}
}